package merchant.space;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

public class ViewCollectionActivity extends Activity {
	
	private Collection collection;
	private ViewCollectionActivity context;
	
	private TextView name;
	private TextView user,user_label;
	private TextView description;
	private GridView gridview;
	private ViewItemsAdapter adapter;
	
	private static final int LOADING = 0;
	private ProgressDialog loadingdialog = null;
	
	public int position;
	
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  
	  setContentView(R.layout.myviewcollection);
  	  context=this;
  	  
  	  name = (TextView)findViewById(R.id.title);
	  description = (TextView)findViewById(R.id.description);
	  user = (TextView)findViewById(R.id.cattegory);
	  user_label = (TextView)findViewById(R.id.cattegory_label);
	  user_label.setText("Owner:");
	  
	  gridview = (GridView) findViewById(R.id.gridview);
	  gridview.setOnItemClickListener(new OnItemClickListener() 
	    {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
	        {
	        	context.position=position;
	        	showDialog(LOADING);
	    		new Thread(){ public void run() {
	    			
	    			DataLink.prepareBrowsedItem(context.position);
	    			threadHandler.sendEmptyMessage(0);
	    			  }}.start();
	    	}
	    });

  	  
  	  collection=DataLink.getBrowsedCollection();
	  
	  refresh();
	  
	}
	  
	  
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOADING:
			loadingdialog = new ProgressDialog(getParent());
			loadingdialog.setTitle("Merchant");
			loadingdialog.setMessage("Loading data");
			return loadingdialog;
			
			
		default:
			return null;
		}
	}
	
	
public void	startItemView()
{
	Intent viewitem = new Intent(context.getParent(), ViewItemActivity.class);
	BrowseGroupActivity parentActivity = (BrowseGroupActivity) context.getParent();
	parentActivity.startChildActivity("ViewItemActivity", viewitem);
	
}
	
public void refresh()
{
	  
	 name.setText(collection.getName());
	 description.setText(collection.getDescription());
	 user.setText(collection.getUser());
	 
	 
	 adapter=new ViewItemsAdapter(context,collection);
	 gridview.setAdapter(adapter);
	 setListViewHeightBasedOnChildren(gridview);
	 
	 name.setFocusableInTouchMode(true);
 	 name.requestFocus();
 		

}
  	

 

	private final Handler threadHandler = new Handler() {
		@Override
		public void handleMessage(Message arg0) {
			//dismissDialog(LOADING);
			 Log.d("ARG123","------------------->"+String.valueOf(arg0.what));
			switch(arg0.what)
			{
				
				case 0:
				{
					dismissDialog(LOADING);
					//refresh();
					startItemView();
				} break;
				
				default:
				{
					 Log.d("aaah","FUCK");
				} break;
				
			
		}
	};
	};
	
	 public void setListViewHeightBasedOnChildren(GridView listView) {
	       
			
		 
		 int rows = adapter.getCount()/3; 
		
		 int rest = adapter.getCount()%3; 
		 if(rest!=0) rows++;
		 Log.d("ROWS",String.valueOf(rows));
		 
			int totalHeight = 0;
	        int columncount=0;
	        int maxheight=0;
	        
	     int verticalspace = 50;
	       listView.setVerticalSpacing(verticalspace);
	        for (int i = 0; i < adapter.getCount(); i++) {
	            
	            View listItem = adapter.getView(i, null, listView);
	            listItem.measure(0, 0);
	            Log.d("MEASSURED",String.valueOf(listItem.getMeasuredHeight()));
	            if(columncount==0)
	            	maxheight=listItem.getMeasuredHeight();
	            else
	            	if(listItem.getMeasuredHeight()>maxheight)
	            		maxheight=listItem.getMeasuredHeight();
	            
	            if(columncount==2)
	            {
	            	totalHeight+=maxheight;
	            	columncount=0;
	            }
	            else columncount++;
	        }
	        
	        
	        if (columncount!=0)
	        	totalHeight+=maxheight;
	        	
	        for(int i=0;i<rows;i++)
	        	totalHeight+=verticalspace;

	        ViewGroup.LayoutParams params = listView.getLayoutParams(); 
	        params.height = totalHeight;// + 20;// + ( (adapter.getCount() - 1));
	        listView.setLayoutParams(params);
	        listView.requestLayout();
	    }
	}
